package com.listener.dao.impl;

import com.listener.dao.PokemonDao;
import com.listener.pojo.Move;
import com.listener.pojo.Pokemon;
import com.listener.pojo.Page;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class PokemonDaoImpl implements PokemonDao {
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate=jdbcTemplate;
    }
    public List<Pokemon> selectAllPokemon() {
        String sql="select * from pokemon";
        RowMapper<Pokemon> rowMapper = new BeanPropertyRowMapper<Pokemon>(Pokemon.class);
        return this.jdbcTemplate.query(sql, rowMapper);
    }
    public List<Pokemon> selectPokemon(Pokemon pokemon) {
        RowMapper<Pokemon> rowMapper = new BeanPropertyRowMapper<Pokemon>(Pokemon.class);
        String sql="select * from pokemon where 1=1 ";
        if(pokemon.getId()!=null){
            sql+="and id='"+pokemon.getId()+"' ";
        }
        if(pokemon.getPokemonId()!=null){
            sql+="and pokemon_id='"+pokemon.getPokemonId()+"' ";
        }
        if(pokemon.getPokemon()!=null){
            sql+="and pokemon='"+pokemon.getPokemon()+"' ";
        }
        return this.jdbcTemplate.query(sql,rowMapper);
    }

    public List<Map<String,Object>>  selectMoveByPokemon(String pokemon) {
        String sql="select * from pokemon_move_earn where pokemon='"+pokemon+"'";
        return this.jdbcTemplate.queryForList(sql);
    }

    public Page<Pokemon> selectPokemonPage(Pokemon pokemon, Integer pageNum, Integer pageSize) {
        Page<Pokemon> page=new Page<Pokemon>();
        String sql=" SELECT CEIL(COUNT(*) / ?) as pageSum FROM pokemon where 1=1 ";
        sql = getString(pokemon, sql);
        RowMapper<Pokemon> rowMapper = new BeanPropertyRowMapper<Pokemon>(Pokemon.class);
        page.setPageSum(Integer.parseInt(this.jdbcTemplate.queryForMap(
                sql,new Object[]{pageSize}).get("pageSum").toString()));
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        sql="select * from pokemon where 1=1 ";
        sql = getString(pokemon, sql);
        sql+=" limit "+(pageNum-1)*pageSize+","+pageSize;
        page.setPage(this.jdbcTemplate.query(sql,rowMapper));
        return page;
    }

    private String getString(Pokemon pokemon, String sql) {
        if(pokemon.getPokemon()!=null){
            sql+="and pokemon like CONCAT('%"+pokemon.getPokemon()+"%') ";
        }
        if(pokemon.getType()!=null && pokemon.getType1()==null){
            sql+="and (type='"+pokemon.getType()+"' "+
                    "or type1='"+pokemon.getType()+"') ";
        }else if(pokemon.getType()!=null && pokemon.getType1()!=null)
        {
            sql+="and ((type='"+pokemon.getType()+"' "+
                    "and type1='"+pokemon.getType1()+"')" +
                    "or (type='"+pokemon.getType1()+"' and " +
                    "type1='"+pokemon.getType()+"') " +
                    ") ";
        }
        if(pokemon.getAbility()!=null){
            sql+="and ( ability like CONCAT('%"+pokemon.getAbility()+"%')  "+
                "or ability1 like CONCAT('%"+pokemon.getAbility()+"%') "+
                    "or hidden_ability like CONCAT('%"+pokemon.getAbility()+"%'))";
        }
        return sql;
    }
}
